From 71331acb085c1304be96d3f58313b518abbbbda2 Mon Sep 17 00:00:00 2001 From: Keir Fraser Date: Wed, 26 Mar 2008 14:44:21 +0000 Subject: [PATCH] ioemu: Fix L1 table endianess of qcow images created by tapdisk The qemu/ioemu implementation of the qcow format uses a big endian L1 table. tapdisk omits the necessary conversion, so qcow images have the wrong endianess and cannot be read by correct implementations of qcow. This patch detects broken tapdisk images and converts their L1 tables to big endian when the image file is opened in ioemu for the first time. The fixed image has a new flag EXTHDR_L1_BIG_ENDIAN set in the extended header. Note that a converted image cannot be opened by tapdisk again. Signed-off-by: Kevin Wolf --- tools/ioemu/block-qcow.c | 58 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tools/ioemu/block-qcow.c b/tools/ioemu/block-qcow.c index 458450dc08..451d28639c 100644 --- a/tools/ioemu/block-qcow.c +++ b/tools/ioemu/block-qcow.c @@ -37,6 +37,11 @@ #define QCOW_OFLAG_COMPRESSED (1LL << 63) +#define XEN_MAGIC (('X' << 24) | ('E' << 16) | ('N' << 8) | 0xfb) + +#define EXTHDR_SPARSE_FILE 0x01 +#define EXTHDR_L1_BIG_ENDIAN 0x02 + typedef struct QCowHeader { uint32_t magic; uint32_t version; @@ -50,6 +55,14 @@ typedef struct QCowHeader { uint64_t l1_table_offset; } QCowHeader; +/*Extended header for Xen enhancements*/ +typedef struct QCowHeader_ext { + uint32_t xmagic; + uint32_t cksum; + uint32_t min_cluster_alloc; + uint32_t flags; +} QCowHeader_ext; + #define L2_CACHE_SIZE 16 typedef struct BDRVQcowState { @@ -137,6 +150,51 @@ static int qcow_open(BlockDriverState *bs, const char *filename, int flags) if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) != s->l1_size * sizeof(uint64_t)) goto fail; + + /* Try to detect old tapdisk images. They have to be fixed because they + * don't use big endian but native endianess for the L1 table */ + if (header.backing_file_offset == 0 && s->l1_table_offset % 4096 == 0) { + + QCowHeader_ext exthdr; + uint64_t l1_bytes = s->l1_size * sizeof(uint64_t); + + if (bdrv_pread(s->hd, sizeof(header), &exthdr, sizeof(exthdr)) + != sizeof(exthdr)) + goto end_xenhdr; + + be32_to_cpus(&exthdr.xmagic); + if (exthdr.xmagic != XEN_MAGIC) + goto end_xenhdr; + + be32_to_cpus(&exthdr.flags); + if (exthdr.flags & EXTHDR_L1_BIG_ENDIAN) + goto end_xenhdr; + + /* The image is broken. Fix it. */ + fprintf(stderr, "qcow: Converting image to big endian L1 table\n"); + + for(i = 0;i < s->l1_size; i++) { + cpu_to_be64s(&s->l1_table[i]); + } + + if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, + l1_bytes) != l1_bytes) { + fprintf(stderr, "qcow: Failed to write new L1 table\n"); + goto fail; + } + + exthdr.flags |= EXTHDR_L1_BIG_ENDIAN; + cpu_to_be32s(&exthdr.flags); + + if (bdrv_pwrite(s->hd, sizeof(header), &exthdr, sizeof(exthdr)) + != sizeof(exthdr)) { + fprintf(stderr, "qcow: Failed to write extended header\n"); + goto fail; + } + } +end_xenhdr: + + /* L1 table is big endian now */ for(i = 0;i < s->l1_size; i++) { be64_to_cpus(&s->l1_table[i]); } -- 2.30.2